package com.example.canopytemperature;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {
    //没有网络
    private static final int NETWORK_NONE=1;
    //移动网络
    private static final int NETWORK_MOBILE=0;
    //无线网络
    private static final int NETWORW_WIFI=2;
    //获取网络启动
    public static int getNetWorkStart(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                //连接服务 CONNECTIVITY_SERVICE
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        //网络信息 NetworkInfo
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        if (activeNetworkInfo!=null&&activeNetworkInfo.isConnected()){
            //判断是否是wifi
            if (activeNetworkInfo.getType()==(ConnectivityManager.TYPE_WIFI)){
                //返回无线网络
                Toast.makeText(context, "当前处于无线网络", Toast.LENGTH_SHORT).show();
                return NETWORW_WIFI;
                //判断是否移动网络
            }else if (activeNetworkInfo.getType()==(ConnectivityManager.TYPE_MOBILE)){
                Toast.makeText(context, "当前处于移动网络", Toast.LENGTH_SHORT).show();
                //返回移动网络
                return NETWORK_MOBILE;
            }
        }else {
            //没有网络
            Toast.makeText(context, "当前没有网络", Toast.LENGTH_SHORT).show();
            return NETWORK_NONE;
        }
        //默认返回  没有网络
        return NETWORK_NONE;
    }

    public static SharedPreferences sp ;
    private static SharedPreferences init(Context context,String FILE_NAME) {
        return sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
    }
    public static void putLong(Context context, String equ_id,String key, long value) {
        SharedPreferences.Editor editor = init(context,equ_id).edit();
        editor.putLong(key, value);
        editor.commit();
    }
    public static long getLong(Context context,String equ_id, String key) {
        return  init(context,equ_id).getLong(key,60000);
    }
    public static void putInt(Context context, String equ_id,String key, int value) {
        SharedPreferences.Editor editor = init(context,equ_id).edit();
        editor.putInt(key, value);
        editor.commit();
    }
    public static int getInt(Context context,String equ_id, String key) {
        return  init(context,equ_id).getInt(key,0);
    }
    public static void putFloat(Context context,String equ_id,@NonNull String key,@NonNull float value) {
        SharedPreferences.Editor editor = init(context,equ_id).edit();
        editor.putFloat(key, value);
        editor.commit();
    }
    public static float getFloat(Context context,String equ_id,@NonNull String key) {
       return  init(context,equ_id).getFloat(key,Float.parseFloat("0"));
    }
    public static void putBoolean(Context context,String equ_id,@NonNull String key,@NonNull boolean value) {
        SharedPreferences.Editor editor = init(context,equ_id).edit();
        editor.putBoolean(key, value);
        editor.commit();
    }
    public static boolean getBoolean(Context context,String equ_id,@NonNull String key) {
        return  init(context,equ_id).getBoolean(key,false);
    }
    public  static void  clear(Context context,String equ_id){
        init(context,equ_id).edit().clear().commit();

    }
    public static String strToDateLong(String pattern, String strDate) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new SimpleDateFormat(pattern).format(date);
    }



}
